﻿using System.Xml.Linq;

namespace EasyXaf.DynamicForm.Models;

public class Form : Panel
{
    public Locations? CaptionLocation { get; set; }

    public Form()
        : base(null, null)
    {
        Form = this;
    }

    public override void WriteXml(XElement element)
    {
        base.WriteXml(element);

        if (CaptionLocation.HasValue)
        {
            SetXmlAttributeValue(element, "caption-location", CaptionLocation switch
            {
                Locations.Left => "left",
                Locations.Top => "top",
                _ => "default",
            });
        }
    }

    public override void ReadXml(XElement element)
    {
        base.ReadXml(element);

        CaptionLocation = GetXmlAttributeValue<string>(element, "caption-location") switch
        {
            "left" => Locations.Left,
            "top" => Locations.Top,
            _ => null,
        };
    }

    public void FromXml(string xml)
    {
        var element = XElement.Parse(xml);
        ReadXml(element);
    }

    public string ToXml()
    {
        var element = CreateXElement();
        WriteXml(element);
        return element.ToString();
    }

    public override XElement CreateXElement()
    {
        return new XElement("form");
    }
}
